<?PHP
/**
 * 控制器处理类 通用选择窗体
 */
class selection_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	
	}


	##################################
	/* 用户选择，多个  */
	##################################
	function usermany__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			 $_POST['start']			= 0;
			if(!isset($_POST['limit']))			 $_POST['limit']			= 10;
			if(!isset($_POST['sort']))		   $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['loadtype']))   $_POST['loadtype']  = 'search'; 
			if(!isset($_POST['type_id']))  	 $_POST['type_id']  = ''; 
			if(!isset($_POST['state']))  	 $_POST['state']  = ''; 
			if(!isset($_POST['classes']))  	 $_POST['classes']  = ''; 
			if(!isset($_POST['sendweek']))  	 $_POST['sendweek']  = ''; 
			if(!isset($_POST['sendtype']))  	 $_POST['sendtype']  = ''; 
			if(!isset($_POST['val']))  	 		 $_POST['val']  = '';
			switch($_POST['loadtype']){
				case "search":
					
					#查询条件
					$sql_where = ' where  a.usertype=2 and a.state=1 and a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.username','a.name','a.phone','a.edate' ));
					#排序信息
					$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
					#先查总数 
					$sql = "
					select 
					count(a.id) 
					from `t_user` a 
					".$sql_where ;  
					$db = factory::getDBO();
					$rows = $db->LoadResult($sql); 
					if($rows){
						#检索出具体数据
						$sql = "select
							a.*,
							b.val as classes
						from `t_user` a 
						left join t_list_user_classes b on a.classes=b.id
						".$sql_where.$sql_order;
						$rs = $db->LoadAssocList($sql);
						#输出结果
						echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
					}else{
						echo '({"total":"0","results":[]})';
					}
					exit();
					break; 
				case "group":
					#查询条件
					$sql_where = ' where  a.usertype=2 and a.deleted=0 ';
					
					if($_POST['state'] != ''){
						$sql_where .= ' and a.state="'.$_POST['state'].'" ';
					}
					if($_POST['sendweek'] != ''){
						$sql_where .= ' and ( a.sendweek1="'.$_POST['sendweek'].'" or a.sendweek2="'.$_POST['sendweek'].'" ) ';
					}
					if($_POST['classes'] != ''){
						$sql_where .= ' and a.classes="'.$_POST['classes'].'" ';
					}
					if($_POST['sendtype'] != ''){
						$sql_where .= ' and a.sendtype="'.$_POST['sendtype'].'" ';
					}
					#排序信息
					$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
					#先查总数 
					$sql = "
					select 
					count(a.id) 
					from `t_user` a 
					".$sql_where ;  
					$db = factory::getDBO();
					$rows = $db->LoadResult($sql); 
					if($rows){
						#检索出具体数据
						$sql = "select
							a.*,
							b.val as classes
						from `t_user` a 
						left join t_list_user_classes b on a.classes=b.id
						".$sql_where.$sql_order;
						$rs = $db->LoadAssocList($sql);
						#输出结果
						echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
					}else{
						echo '({"total":"0","results":[]})';
					}
					exit();
					
					break;
				case "classes":
					#查询条件
					$sql_where = ' where  a.usertype=2 and a.state=1 and a.deleted=0 and a.classes="'.$_POST['classes'].'" ';
					#排序信息
					$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
					#先查总数 
					$sql = "
					select 
					count(a.id) 
					from `t_user` a 
					".$sql_where ;  
					$db = factory::getDBO();
					$rows = $db->LoadResult($sql); 
					if($rows){
						#检索出具体数据
						$sql = "select
							a.*,
							b.val as classes
						from `t_user` a 
						left join t_list_user_classes b on a.classes=b.id
						".$sql_where.$sql_order;
						$rs = $db->LoadAssocList($sql);
						#输出结果
						echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
					}else{
						echo '({"total":"0","results":[]})';
					}
					exit();
					
					break;
				default://all
					#查询条件
					$sql_where = ' where  a.usertype=2 and a.state=1 and a.deleted=0 ';
					#排序信息
					$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
					#先查总数 
					$sql = "
					select 
					count(a.id) 
					from `t_user` a 
					".$sql_where ;  
					$db = factory::getDBO();
					$rows = $db->LoadResult($sql); 
					if($rows){
						#检索出具体数据
						$sql = "select
							a.*,
							b.val as classes
						from `t_user` a 
						left join t_list_user_classes b on a.classes=b.id
						".$sql_where.$sql_order;
						$rs = $db->LoadAssocList($sql);
						#输出结果
						echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
					}else{
						echo '({"total":"0","results":[]})';
					}
					exit();
			}
			exit();
		}	
		$_GET=format::addslash_deep($_GET);
		# 回调函数 funcname 
		if(isset($_GET['funcname'])){
			$funcname = $_GET['funcname'];
		}else{
			$funcname = '';
		}
		$this->assign('funcname',$funcname);
		$this->assign('rs_group',array(
			'星期一',
			'星期二',
			'星期三',
			'星期四',
			'星期五',
			'星期六',
			'星期日'
		));
		$this->assign('rs_classes',factory::module('list')->getlist('user_classes') );
	}
	
	
	
	##################################
	/* 选取菜品  */
	##################################
	function vegemany__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			 $_POST['start']			= 0;
			if(!isset($_POST['limit']))			 $_POST['limit']			= 10;
			if(!isset($_POST['sort']))		   $_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				 $_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = ''; 
			if(!isset($_POST['loadtype']))   $_POST['loadtype']  = 'search'; 
			if(!isset($_POST['type_id']))  	 $_POST['type_id']  = ''; 
			switch($_POST['loadtype']){
				case "search":
					
					#查询条件
					$sql_where = ' where    a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.name','a.code','b.type_name' ));
					#排序信息
					$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
					#先查总数 
					$sql = "
					select 
					count(a.id) 
					from `t_vege` a    left join t_vegetype b on a.type_id=b.id  
					".$sql_where ;  
					$db = factory::getDBO();
					$rows = $db->LoadResult($sql); 
					if($rows){
						#检索出具体数据
						$sql = "select
							a.*,
							b.type_name
						from `t_vege` a  left join t_vegetype b on a.type_id=b.id  
						".$sql_where.$sql_order;
						$rs = $db->LoadAssocList($sql);
						#输出结果
						echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
					}else{
						echo '({"total":"0","results":[]})';
					}
					exit();
	
					break; 
				case "group":
					#查询条件
					$sql_where = ' where    a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.name','b.type_name' ));
					if($_POST['type_id'] != ''){
						$sql_where .= ' and a.type_id="'.$_POST['type_id'].'" ';
					}
					#排序信息
					$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
					#先查总数 
					$sql = "
					select 
					count(a.id) 
					from `t_vege` a    left join t_vegetype b on a.type_id=b.id  
					".$sql_where ;  
					$db = factory::getDBO();
					$rows = $db->LoadResult($sql); 
					if($rows){
						#检索出具体数据
						$sql = "select
							a.*,
							b.type_name
						from `t_vege` a  left join t_vegetype b on a.type_id=b.id  
						".$sql_where.$sql_order;
						$rs = $db->LoadAssocList($sql);
						#输出结果
						echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
					}else{
						echo '({"total":"0","results":[]})';
					}
					exit();
					break;
				default://all
					#查询条件
					$sql_where = ' where    a.deleted=0 ';
					#排序信息
					$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
					#先查总数 
					$sql = "
					select 
					count(a.id) 
					from `t_vege` a    left join t_vegetype b on a.type_id=b.id  
					".$sql_where ;  
					$db = factory::getDBO();
					$rows = $db->LoadResult($sql); 
					if($rows){
						#检索出具体数据
						$sql = "select
							a.*,
							b.type_name
						from `t_vege` a  left join t_vegetype b on a.type_id=b.id  
						".$sql_where.$sql_order;
						$rs = $db->LoadAssocList($sql);
						#输出结果
						echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
					}else{
						echo '({"total":"0","results":[]})';
					}
					exit();	
			}		
			exit();	
		}
		$_GET=format::addslash_deep($_GET);
		# 回调函数 funcname 
		if(isset($_GET['funcname'])){
			$funcname = $_GET['funcname'];
		}else{
			$funcname = '';
		}
		$this->assign('funcname',$funcname);
		$m_vege =  factory::module('vege'); 
		$rs_vegetype = $m_vege->getalltype();
		$this->assign('rs_vegetype',$rs_vegetype);
	}

	##################################
	/* 菜品套餐选择  */
	##################################
	function vegecombo__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 1000;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['sortval']))     $_POST['sortval']  = '';
			$nowdate = date('Y-m-d H:i:s');
  	  #查询条件
			$sql_where = ' where a.deleted=0  and a.publishs=1  and a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ';
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_combo` a 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*
				from `t_combo` a  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as &$val){
					$val['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$val['id'].'" ');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}	
		$_GET=format::addslash_deep($_GET);
		# 回调函数 funcname 
		if(isset($_GET['funcname'])){
			$funcname = $_GET['funcname'];
		}else{
			$funcname = '';
		}
		$this->assign('funcname',$funcname);
		 
	}

	##################################
	/* 菜品套餐选择-后台  */
	##################################
	function vegecombo_byadmin__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 1000;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['sortval']))     $_POST['sortval']  = '';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['isend']))   $_POST['isend']  = ''; 
			if(!isset($_POST['publishs']))   $_POST['publishs']  = '';
			$nowdate = date('Y-m-d H:i:s');
  	  #查询条件
			$sql_where = ' where a.deleted=0  '.func::speedsearch($_POST['keywords'],array( 'a.name' ));
			if($_POST['publishs'] != ''){
				$sql_where .= ' and a.publishs="'.$_POST['publishs'].'" ';
			}
			if($_POST['isend'] != ''){
				if($_POST['isend'] == '1'){
					$sql_where .= ' and a.edate<"'.date('Y-m-d H:i:s').'" ';
				}else{
					$sql_where .= ' and a.edate>="'.date('Y-m-d H:i:s').'" ';
				}
			}
			
			
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_combo` a 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*
				from `t_combo` a  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				foreach($rs as &$val){
					$val['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$val['id'].'" ');
				}
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}	
		$_GET=format::addslash_deep($_GET);
		# 回调函数 funcname 
		if(isset($_GET['funcname'])){
			$funcname = $_GET['funcname'];
		}else{
			$funcname = '';
		}
		$this->assign('funcname',$funcname);
		 
	}	
	/* 组合套餐选择 */
	public function vegecombo_sltbuy__(){
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		
		//判断是否可以购买
		$nowdate = date('Y-m-d H:i:s');
		//$rs_combo = $db->loadassoc('select a.* from `t_combo` a where id="'.$id.'" and  a.deleted=0  and a.publishs=1  and a.type=1 and a.bdate<="'.$nowdate.'" and a.edate>="'.$nowdate.'" ');
		$rs_combo = $db->loadassoc('select a.* from `t_combo` a where id="'.$id.'" and  a.deleted=0     and a.type=1  ');
		if(!$rs_combo){
			exit('未找到商品信息');
		}
		$rs_combo['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$rs_combo['id'].'" ');
		$this->assign('rs_combo',$rs_combo);
		# 菜品信息
		$this->assign('rs_veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
		# 回调函数 funcname 
		if(isset($_GET['funcname'])){
			$funcname = $_GET['funcname'];
		}else{
			$funcname = '';
		}
		$this->assign('funcname',$funcname);
	}
	
	
	
	
}
?>